package com.wuyan.locationdog.ui.my


import android.text.Editable
import android.text.TextWatcher
import android.widget.RadioButton
import androidx.activity.viewModels
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.bean.DeviceBeanContent
import com.arcsoft.commonbase.bus.Bus
import com.arcsoft.commonbase.bus.bus_device_select
import com.arcsoft.commonbase.ext.ImageOptions
import com.arcsoft.commonbase.ext.btnEnable
import com.arcsoft.commonbase.ext.customShowLong
import com.arcsoft.commonbase.ext.fileToPart
import com.arcsoft.commonbase.ext.loadImage
import com.arcsoft.commonbase.ext.textString
import com.arcsoft.commonbase.ext.textStringTrim
import com.blankj.utilcode.util.ToastUtils
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import com.wuyan.locationdog.databinding.MyFeedbackActivityBinding
import com.wuyan.locationdog.help.workOrderTypeNum
import com.wuyan.locationdog.util.imageselecter.GlideEngine
import com.wuyan.locationdog.util.imageselecter.ImageCompressEngine
import com.wuyan.locationdog.viewmodel.MyViewModel
import okhttp3.RequestBody
import java.io.File

class MyFeedbackActivity : BaseBindingActivity<MyFeedbackActivityBinding>(), TextWatcher {
    private var fileUrl: String? = null
    private var deviceId: String = ""
    private val mViewModel: MyViewModel by viewModels()
    var lists = mutableListOf<RadioButton>()

    override fun initData() {

        setTitleText("问题反馈")

        binding.apply {

            etTitle.addTextChangedListener(this@MyFeedbackActivity)
            etDesc.addTextChangedListener(this@MyFeedbackActivity)

            btnSure.setOnClickListener {
                val map = HashMap<String, Any>()
                map["title"] = etTitle.textStringTrim()
                map["content"] = etDesc.textStringTrim()

                fileUrl?.let {
                    map["img"] = fileUrl.toString()
                }
                map["workOrderType"] = workOrderTypeNum(lists.find { it.isChecked }?.textString() ?: "")
                mViewModel.addWorkOrder(map)
            }
            btnAlbum.setOnClickListener {
                PictureSelector.create(mActivity)
                    .openGallery(SelectMimeType.ofImage())
                    .setImageEngine(GlideEngine())
//                    .setCropEngine(ImageCropEngine())
                    .setCompressEngine(ImageCompressEngine())
                    .setMaxSelectNum(1)
                    .forResult(object : OnResultCallbackListener<LocalMedia> {
                        override fun onResult(result: ArrayList<LocalMedia>) {
                            val file = File(result[0].availablePath)
                            btnAlbum.loadImage(result[0].availablePath, ImageOptions().apply {

                            })
                            val map = HashMap<String, RequestBody>()
                            mViewModel.fileUpload(map, fileToPart(file))
                        }

                        override fun onCancel() {}
                    })
            }

            radioGroup1.setOnCheckedChangeListener { group, checkedId ->

                if (rb1.isChecked) {
                    radioGroup2.clearCheck()
                }
                if (rb2.isChecked) {
                    radioGroup2.clearCheck()
                }
                if (rb3.isChecked) {
                    radioGroup2.clearCheck()
                }


            }
            radioGroup2.setOnCheckedChangeListener { group, checkedId ->
                if (rb4.isChecked) {
                    radioGroup1.clearCheck()
                }
                if (rb5.isChecked) {
                    radioGroup1.clearCheck()
                }
                if (rb6.isChecked) {
                    radioGroup1.clearCheck()
                }

            }
            lists.add(rb1)
            lists.add(rb2)
            lists.add(rb3)
            lists.add(rb4)
            lists.add(rb5)
            lists.add(rb6)
        }
    }


    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {


    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

    }

    override fun afterTextChanged(s: Editable?) {
        binding.apply {

            btnEnable(btnSure, etTitle, etDesc)

            textNum.text = etDesc.length().toString() + "/500"
        }
    }


    override fun observe() {
        super.observe()

        Bus.observe<DeviceBeanContent>(bus_device_select, this) {
            deviceId = it.deviceId

        }

        mViewModel.anyBean.observe(this) {
            customShowLong("提交成功")

            finish()
        }



        mViewModel.fileUploadBean.observe(this) {


            fileUrl = it.content.fileUrl

        }
    }
}